﻿using System.Collections.Generic;
using System.Linq;
using Xenta.Api;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Security;
using Xenta.Utils;

namespace Xenta.Operations
{
    #region __General__

    /// <summary>
    /// Creates a sales history record.
    /// </summary>
    public class CreateSalesRuleHistory : CreateAuditable<SalesRuleHistoryEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("SALES");
            }
        }

        /// <summary>
        /// Gets or sets the rule identifier.
        /// </summary>
        public int RuleID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the order identifier.
        /// </summary>
        public int OrderID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the order item identifier.
        /// </summary>
        public int ItemID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public SalesRuleHistoryFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Construct(IUnitOfWork uow, SalesRuleHistoryEntity entity)
        {
            base.Construct(uow, entity);

            entity.Rule = uow.Execute(new GetSalesRule
            {
                ThrowsException = true,
                EntityID = RuleID
            }).Result;
            entity.Order = uow.Execute(new GetOrder
            {
                ThrowsException = true,
                EntityID = OrderID
            }).Result;
            entity.Item = ItemID != 0 
                ? uow.Execute(new GetOrderItem
                {
                    ThrowsException = true,
                    EntityID = ItemID
                }).Result 
                : null;
            entity.Flags = Flags;
        }

        #endregion
    }

    /// <summary>
    /// Updates the sales history record.
    /// </summary>
    public class UpdateSalesRuleHistory : UpdateAuditable<SalesRuleHistoryEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("SALES");
            }
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public SalesRuleHistoryFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Revise(IUnitOfWork uow, SalesRuleHistoryEntity entity)
        {
            base.Revise(uow, entity);

            entity.Flags = Flags;
        }

        #endregion
    }

    /// <summary>
    /// Deletes the sales history record.
    /// </summary>
    public class DeleteSalesRuleHistory : DeleteEntity<SalesRuleHistoryEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("SALES");
            }
        }

        #endregion
    }

    /// <summary>
    /// Gets the sales history record by identifier.
    /// </summary>
    public class GetSalesRuleHistory : GetEntity<SalesRuleHistoryEntity>,
        IApiOperation
    {
    }

    #endregion

    #region __Search__

    /// <summary>
    /// Search over sales rule history.
    /// </summary>
    public class SalesRuleHistorySearch : SearchOperation<SalesRuleHistoryEntity>,
        IApiOperation
    {
        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public SalesRuleHistorySearch()
        {
            SortBy = new []
            {
                SalesRuleHistorySortBy.ID
            };
            SortDir = SortDirection.Ascending;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the rule identifier.
        /// </summary>
        public int? RuleID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the order identifier.
        /// </summary>
        public int? OrderID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the item identifier.
        /// </summary>
        public int? ItemID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flag mask.
        /// </summary>
        public SalesRuleHistoryFlags FlagMask
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the sort by.
        /// </summary>
        public SalesRuleHistorySortBy[] SortBy
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the sort direction.
        /// </summary>
        public SortDirection SortDir
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override IQueryable<SalesRuleHistoryEntity> Build(IUnitOfWork uow, IQueryable<SalesRuleHistoryEntity> query)
        {
            query = base.Build(uow, query);
            query = query.FilterByFlags(x => (int)x.Flags, (int)FlagMask);
            if(RuleID.HasValue)
                query = query.Where(x => x.Rule.EntityID == RuleID.Value);
            if(OrderID.HasValue)
                query = query.Where(x => x.Order.EntityID == OrderID.Value);
            if(ItemID.HasValue)
                query = query.Where(x => x.Item.EntityID == ItemID.Value);
            if(SortBy != null)
            {
                for(int i = 0; i < SortBy.Length; i++)
                {
                    switch(SortBy[i])
                    {
                        case SalesRuleHistorySortBy.CreatedOn:
                            query = query.OrderBy(x => x.CreatedOn, SortDir, i != 0);
                            break;
                        default:
                            query = query.OrderBy(x => x.EntityID, SortDir, i != 0);
                            break;
                    }
                }
            }
            return query;
        }

        #endregion
    }

    #endregion
}
